package com.redrose.audioandvideo.camera;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.redrose.audioandvideo.R;
import com.redrose.audioandvideo.bean.Photo;
import com.redrose.audioandvideo.databinding.ActivityBigImageBinding;
import com.redrose.audioandvideo.pagetransformer.RotateDownPageTransformer;
import com.redrose.audioandvideo.ui.activitys.BaseActivity;
import com.redrose.audioandvideo.utils.GlideUtil;
import com.redrose.audioandvideo.widget.ResizableImageView;

import org.litepal.crud.DataSupport;

import java.util.List;
@Route(path = "/activity/bigimageactivity")
public class BigImageActivity extends BaseActivity<ActivityBigImageBinding> {
    private List<Photo> mPhotoList;
    private int mPosition;
    private int mCurrentMax;
    private ImagePagerAdapter mAdapter;
    @Override
    public void initData() {
        Photo specifiefPhoto = (Photo) getIntent().getParcelableExtra("photoData");
        mPhotoList = DataSupport.order("classId desc").find(Photo.class);
        mCurrentMax = mPhotoList.size();
        getRealPosition(specifiefPhoto);
        initAdapter();
        initEvent();
    }

    private void initEvent() {
        mBinding.imageViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                refrashPosition(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void getRealPosition(Photo p) {
        if (p == null) {
            mPosition = 0;
            return;
        }
        int size = mPhotoList.size();
        for (int i = 0; i < size; i++) {
            Photo photo = mPhotoList.get(i);
            if (p.getTime().equals(photo.getTime())) {
                mPosition = i;
                break;
            }
        }
    }
    private void initAdapter() {
        mAdapter = new ImagePagerAdapter();
        mBinding.imageViewPager.setAdapter(mAdapter);
        /** 翻页动画设置 */
        /*mViewPager.setPageTransformer(false,new ZoomOutPageTransformer());*/
        /*mViewPager.setPageTransformer(false,new DepthPageTransformer());*/
        mBinding.imageViewPager.setPageTransformer(false, new RotateDownPageTransformer());
        mBinding.imageViewPager.setCurrentItem(mPosition);
        refrashPosition(mPosition);
    }
    private void refrashPosition(int position) {
        position = position + 1;
        String statues = String.format("%d / %d", position, mCurrentMax);
        mBinding.imagePosition.setText(statues);
    }
    @Override
    public int setLayout() {
        return R.layout.activity_big_image;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    private class ImagePagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {

            return mCurrentMax;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.image_viewpager_item, null);
            ResizableImageView imageView = view.findViewById(R.id.resize_image);
            GlideUtil.loadImage(BigImageActivity.this, mPhotoList.get(position).getPath(),imageView);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
